﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WebVisualizers.Types;
using System.Collections.Specialized;
using System.Xml.Serialization;
using System.IO;
using WebVisualizers.Helpers;

namespace WebVisualizers.Forms
{
    public partial class VisualizerForm : Form
    {
        private PageData _pageData;

        public VisualizerForm()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// Display the visualizer with Data from the pageData object
        /// </summary>
        /// <param name="pageData"></param>
        public void ShowVisualizer(PageData pageData)
        {
            this.PageData = pageData;
            CreateDataTabs();
            ShowDialog();
        }
        
        /// <summary>
        /// Adds a tabs based on the DataTablelist in PageData
        /// </summary>
        private void CreateDataTabs()
        {
            foreach (DataTable dt in PageData.DataTableList)
            {
                TabPage tabPage = new TabPage(dt.TableName);
                CreateDataGridView(tabPage, dt);
                tabItems.TabPages.Add(tabPage);
            }
        }

        /// <summary>
        /// Adds a datagridview to the tabPage
        /// </summary>
        private void CreateDataGridView(TabPage tab, DataTable dt)
        {
            DataGridView dgv = new DataGridView();
            dgv.AutoGenerateColumns = false;
            dgv.BackgroundColor = Color.White;

            ///Add all columns from the Datatable to the datagridview
            for (var i = 0; i < dt.Columns.Count; i++)
            {
                string columnName = string.Format("{0}",dt.Columns[i]);
                dgv.Columns.Add(columnName, columnName);
                dgv.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgv.Columns[i].DataPropertyName = columnName;
            }

            dgv.Size = tab.Size;
            dgv.AllowUserToAddRows = false;
            dgv.AllowUserToDeleteRows = false;
            dgv.Dock = DockStyle.Fill;
            dgv.DataSource = dt;
            tab.Controls.Add(dgv);
        }

        private void CopyButton_Click(object sender, EventArgs e)
        {
            string xml = DataHelper.GetXmlFromDataTable(this.PageData.DataTableList[tabItems.SelectedIndex]);
            Clipboard.SetText(xml);
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "Xml File|*.xml";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(saveFile.FileName, false))
                {
                    sw.Write(DataHelper.GetXmlFromDataTable(this.PageData.DataTableList[tabItems.SelectedIndex]));
                    sw.Close();
                }
            }
        }

        public PageData PageData
        {
            get { return _pageData; }
            set { _pageData = value; }
        }
    }
}
